<?php

class Activity extends Model {

    private $table = "activity";
    private $id = 0;
    private $user = "";
    private $description = "";
    private $local = "";
    private $data = "";
    private $hora = "";

    public function getTable() {
        return $this->table;
    }

    public function getId() {
        return $this->id;
    }

    public function getUser() {
        return $this->user;
    }

    public function getDescription() {
        return $this->description;
    }

    public function getLocal() {
        return $this->local;
    }

    public function getData() {
        return $this->data;
    }

    public function getHora() {
        return $this->hora;
    }

    public function setId($id) {
        $this->id = $id;
    }

    public function setUser($user) {
        $this->user = $user;
    }

    public function setDescription($description) {
        $this->description = $description;
    }

    public function setLocal($local) {
        $this->local = $local;
    }

    public function setData($data) {
        $this->data = $data;
    }

    public function setHora($hora) {
        $this->hora = $hora;
    }

    public function insert() {
        $query = "insert into $this->table(user,description,local,data,hora) "
                . "values('$this->user','$this->description','$this->local','$this->data','$this->hora')";
        if (mysql_query($query) >= 1) {
            return true;
        } else {
            return false;
        }
    }

    public function update() {
        $query = "update $this->table set user = '$this->user',"
                . "description= '$this->description',"
                . "local = '$this->local',"
                . "data = '$this->data', "
                . "hora = '$this->hora' "
                . "where id = $this->id";
        if (mysql_query($query) >= 1) {
            $user = new User();
            return true;
        } else {
            return false;
        }
    }

    public function delete() {
        $query = "delete from $this->table where id = $this->id";
        if (mysql_query($query) >= 1) {
            return true;
        } else {
            return false;
        }
    }

    public function selectByUser($user) {
        $query = "select * from $this->table where user = $user";
        $result = mysql_query($query);
        return $this->prepareArray($result);
    }

    public function selectById($id) {
        $query = "select * from $this->table where id = $id";
        $result = mysql_query($query);
        $o = mysql_fetch_object($result);
        $this->setId($id);
        $this->setUser($o->user);
        $this->setDescription($o->description);
        $this->setData($o->data);
        $this->setLocal($o->local);
        $this->setHora($o->hora);
    }

    private function prepareArray($result) {
        $activitys = null;
        while ($o = mysql_fetch_object($result)) {
            $c = new Activity();
            $c->setId($o->id);
            $c->setUser($o->user);
            $c->setDescription($o->description);
            $c->setData($o->data);
            $c->setLocal($o->local);
            $c->setHora($o->hora);
            $activitys[] = $c;
        }
        return $activitys;
    }

    public function printData(){
        $data = explode('-', $this->getData());
        return $data[2]."/".$data[1]."/".$data[0];
    }


}

?>
